﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BKH.BLL
{
    /// <summary>
    /// Author :Dao Viet Danh
    /// </summary>
    class UserBUO
    {
        HospitalDBDataContext context = new HospitalDBDataContext();
        public List<tbl_User> GetAll()
        {
            return context.tbl_Users.ToList<tbl_User>();
        }
        /// <summary>
        /// Tao mot user 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <param name="quyen"></param>
        /// <returns></returns>
        public bool InserUser(String userName, String passWord, int quyen)
        {

            tbl_User user = new tbl_User();
            user.UserName = userName;
            user.Password = MaHoa.EncryptMD5(passWord);
            user.Id_Quyen = quyen;

            try
            {
                // Kiem tra ten user co ton tai khong
                var t = context.tbl_Users.Where(n=>n.UserName.ToString().Equals(user.UserName.ToString())).ToList();
                if(t.Count==1)
                    return false;

                context.tbl_Users.InsertOnSubmit(user);
                context.SubmitChanges();
                return true;
                    

            }
            catch (Exception)
            {

                return false;
            }


        }
        

        /// <summary>
        /// Kiem tra mot user
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool CheckUser(String userName, String passWord)
        {
            var t = context.tbl_Users.Where(n => n.UserName.ToString().Equals(userName) && n.Password.ToString().Equals(MaHoa.EncryptMD5(passWord))).ToList();
            return t.Count == 1 ? true : false;
        }
        public bool DeleteUser(String userName)
        {
            tbl_User obj = new tbl_User();
            obj.UserName = userName;

            var t = context.tbl_Users.Where(n => n.UserName.ToString().Equals(obj.UserName.ToString()));
            if (t == null)
                return false;
            context.tbl_Users.DeleteOnSubmit(obj);
            context.SubmitChanges();
            return true;

        }
    
    }
    
}
